<template>
  <div class="tenant-monitoring">
    <div class="monitoring-content">
      <!-- 设备状态面板 -->
      <div class="device-status-container">
        <div class="device-status-overview">
          <div class="device-status-gauge">
            <div class="gauge-display">
              <div class="gauge-number">{{ deviceOnlinePercentage }}%</div>
              <div class="gauge-label">在线率</div>
            </div>
            <div class="gauge-ring">
              <svg viewBox="0 0 120 120">
                <circle class="gauge-ring-bg" cx="60" cy="60" r="54" />
                <circle class="gauge-ring-fill" cx="60" cy="60" r="54" 
                        :stroke-dasharray="`${deviceOnlinePercentage * 3.39} 339`" />

              </svg>
            </div>
          </div>
          <div class="device-status-stats">
            <div class="status-row">
              <div class="status-label">
                <div class="status-dot online"></div>
                <span>在线设备</span>
              </div>
              <div class="status-count">{{ onlineCount }} 台</div>
              <div class="status-bar-container">
                <div class="status-bar online" :style="{width: `${deviceOnlinePercentage}%`}"></div>
              </div>
            </div>
            <div class="status-row">
              <div class="status-label">
                <div class="status-dot offline"></div>
                <span>离线设备</span>
              </div>
              <div class="status-count">{{ offlineCount }} 台</div>
              <div class="status-bar-container">
                <div class="status-bar offline" :style="{width: `${100 - deviceOnlinePercentage}%`}"></div>
              </div>
            </div>
            <div class="status-row total">
              <div class="status-label">
                <i class="el-icon-monitor"></i>
                <span>设备总数</span>
              </div>
              <div class="status-count">{{ totalDeviceCount }} 台</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    smartlocksStatus
  } from "@/api/lock/menu";
  export default {
    data() {
      return {
        onlineCount: 0,
        offlineCount: 0,
        totalDeviceCount: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
      }
    },
    computed: {
      deviceOnlinePercentage() {
        if (this.totalDeviceCount === 0) return 0;
        return Math.round((this.onlineCount / this.totalDeviceCount) * 100);
      }
    },
    created() {
      this.getDeviceStatusData();
    },
    methods: {
      getDeviceStatusData() {
        smartlocksStatus(this.queryParams).then(response => {
          this.onlineCount = response.data?.zaixianCount || 0;
          this.offlineCount = response.data?.lixianCount || 0;
          this.totalDeviceCount = (response.data?.zaixianCount || 0) + (response.data?.lixianCount || 0);
          console.log("设备状态数据:", response.data);
        });
      },
    }
  }
</script>

<style lang="scss" scoped>
.tenant-monitoring {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding: 0;

  .monitoring-content {
    flex: 1;
    overflow: hidden;
    padding: 20px;
    display: flex;
    align-items: center;
    justify-content: center;

         .device-status-container {
       width: 100%;
       background: #ffffff;
       padding: 30px;
     }

    .device-status-overview {
      display: flex;
      align-items: center;
      gap: 40px;
    }

    .device-status-gauge {
      position: relative;
      width: 200px;
      height: 200px;
    }

    .gauge-display {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      z-index: 2;
    }

         .gauge-number {
       font-size: 32px;
       font-weight: bold;
       color: #1890ff;
       margin-bottom: 5px;
     }

     .gauge-label {
       font-size: 14px;
       color: #666666;
     }

    .gauge-ring {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }

         .gauge-ring-bg {
       fill: none;
       stroke: #f0f0f0;
       stroke-width: 8;
     }

    .gauge-ring-fill {
      fill: none;
      stroke: #67c23a;
      stroke-width: 8;
      stroke-linecap: round;
      transform: rotate(-90deg);
      transform-origin: 60px 60px;
      transition: stroke-dasharray 0.5s ease;
    }

    .gauge-text {
      font-size: 12px;
      font-weight: bold;
      text-anchor: middle;
      
      &.online {
        fill: #67c23a;
      }
      
      &.offline {
        fill: #f56c6c;
      }
    }

         .device-status-stats {
       flex: 1;
       color: #333333;
     }

    .status-row {
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      
             &.total {
         margin-top: 30px;
         padding-top: 20px;
         border-top: 1px solid #e8e8e8;
       }
    }

    .status-label {
      display: flex;
      align-items: center;
      width: 120px;
      font-size: 14px;
      
      .status-dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        margin-right: 8px;
        
        &.online {
          background: #67c23a;
        }
        
        &.offline {
          background: #f56c6c;
        }
      }
      
      i {
        margin-right: 8px;
        font-size: 16px;
      }
    }

    .status-count {
      width: 80px;
      text-align: right;
      font-weight: bold;
      font-size: 16px;
    }

         .status-bar-container {
       flex: 1;
       height: 6px;
       background: #f5f5f5;
       border-radius: 3px;
       margin-left: 15px;
       overflow: hidden;
     }

    .status-bar {
      height: 100%;
      border-radius: 3px;
      transition: width 0.5s ease;
      
      &.online {
        background: #67c23a;
      }
      
      &.offline {
        background: #f56c6c;
      }
    }
  }
}
</style>
